package com.example.dt;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DtActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dt);

        //检测实体按键



//广告轮播图

        List images = new ArrayList();
        images.add("http://kwimg2.kuwo.cn/star/upload/66/85/1575256374021_.jpg");
        images.add("http://kwimg2.kuwo.cn/star/upload/71/68/1575818166158_.jpg");
        images.add("http://kwimg1.kuwo.cn/star/upload/68/54/1575429173078_.jpg");


        Banner banner = (Banner) findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        //增加点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(DtActivity.this, "position" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
    //重写onKeyDown方法,对按键(不一定是返回按键)监听
    private double mExitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        当按下两次后
            if (keyCode == KeyEvent.KEYCODE_EISU
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                // 判断间隔时间 大于2秒就退出应用
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    Toast.makeText(getApplicationContext(), "再按一次返回连接界面",
                            Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                }

            //创建okHttpClient对象
            OkHttpClient mOkHttpClient = new OkHttpClient();
      //创建一个Request
            final Request request = new Request.Builder()
                    .url("http://api.zq.ledusoft.cn/api/index/index")
                    .addHeader("Content-Type", "application/json")
                    .build();
      //new call
            Call call = mOkHttpClient.newCall(request);

       //请求加入调度
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    System.out.println(request);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //启动另一个Activity
                    startActivity(new Intent(DtActivity.this,MainActivity.class));
                }

            });
        }
        return false;
    }
}